package Thread;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

/**生产者和消费者模型*/
public class demo30 {
    public static void main(String[] args) {
        BlockingDeque<Integer> queue = new LinkedBlockingDeque<>(1000);
        Thread producer = new Thread(()->{
           int n = 0;
           while(true) {
               try{
                   queue.put(n);
                   System.out.println("生产元素 "+n);
                   n++;
                   // Thread.sleep(1000); // 可控
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
           }
        },"producer");


        Thread consumer = new Thread(()->{
            while(true) {
                try{
                    Integer n = queue.take();
                    System.out.println("输出元素 "+n);
                    // Thread.sleep(1000);  // 可控
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"consumer");

        producer.start();
        consumer.start();

    }
}
